/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package net.dreamlu.db.manager;

import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import net.dreamlu.db.po.*;
import net.dreamlu.db.qo.*;
import net.dreamlu.db.repo.*;
/**
 *
 * @author lambert.wang email:lambert.wang(a)crediteyes.com
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class BlogManager {

	@Autowired
	private BlogRepository blogRepository;

	public List<Blog> queryAll() {
		return blogRepository.findAll();
	}

	/**
	 * 分页查询所有Blog对象
	 * 
	 * @param pageable pageable  Pageable pageable = PageRequest.of(page, size,sort);
	 * @return
	 */
	public Page<Blog> queryBlogByPage(Pageable pageable) {
		return blogRepository.findAll(pageable);
	}

	/**
	 * 根据条件分页查询Blog对象
	 * 
	 * @param pageable
	 * @param query
	 * @return
	 */
	public Page<Blog> queryBlogByPage(Pageable pageable, BlogQuery blogQuery) {
		Specification<Blog> example = blogQuery.createSpecification();
		return blogRepository.findAll(example, pageable);
	}

	/**
	 * 不分页查询所有Blog对象 不分页不带排序
	 * 
	 * @param query
	 * @return
	 */
	public List<Blog> queryBlog(BlogQuery blogQuery) {
		Specification<Blog> example = blogQuery.createSpecification();
		return blogRepository.findAll(example);
	}

	/**
	 * 不分页查询单个Blog对象 不分页不带排序
	 * 
	 * @param query
	 * @return
	 */
	public Blog queryUniqueBlog(BlogQuery blogQuery) {
		Specification<Blog> example = blogQuery.createSpecification();
		Optional<Blog> op = blogRepository.findOne(example);
		if (op.isPresent()) {
			return op.get();
		}
		return null;
	}

	/**
	 * 根据ID查询Blog对象
	 * 
	 * @param id
	 * @return
	 */
	public Blog queryBlogById(java.lang.Long id) {
		Optional<Blog> op = blogRepository.findById(id);
		if (op.isPresent()) {
			return op.get();
		}
		return null;
	}

	/**
	 * 保存Blog对象
	 * 
	 * @param entity
	 */
	public void saveBlog(Blog blog) {
		blogRepository.save(blog);
	}

	/**
	 * 保存多个Blog对象
	 * 
	 * @param entitys
	 */
	public void saveBlog(List<Blog> blogs) {
		blogRepository.saveAll(blogs);
	}

	/**
	 * 批量删除
	 * 
	 * @param deleteList
	 */
	public void deleteBlog(List<Blog> deleteList) {
		blogRepository.deleteAll(deleteList);
	}

	/**
	 * 批量删除根据查询条件
	 * 
	 * @param query
	 */
	public void deleteBlog(BlogQuery blogQuery) {
		blogRepository.deleteInBatch(queryBlog(blogQuery));
	}

	/**
	 * 批量删除根据id
	 * 
	 * @param ids
	 */
	public void deleteBlogByIds(java.lang.Long... ids) {
		for (java.lang.Long id : ids) {
			blogRepository.deleteById(id);
		}
	}

	/**
	 * 删除单个对象
	 * 
	 * @param entity
	 */
	public void deleteBlog(Blog blog) {
		blogRepository.delete(blog);
	}
}
